/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmCurrentAppointment.java
 *
 * arthur mbelisle age 24
 * Created on Jul 5, 2011, 1:19:40 PM
 */
package khmhappointmentsystem;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DateEditor;
import javax.swing.SpinnerDateModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmCurrentAppointment extends javax.swing.JInternalFrame {

    FrmCheckin frmCheckin = null;
    static final Logger logger = Logger.getLogger(FrmCurrentAppointment.class.getName());
    ArrayList<String> firstName = new ArrayList<String>();
    ArrayList<String> lastName = new ArrayList<String>();
    ArrayList<String> id = new ArrayList<String>();
    ArrayList<String> clinic = new ArrayList<String>();
    ArrayList<String> doctor = new ArrayList<String>();
    ArrayList<String> date = new ArrayList<String>();
    ArrayList<String> time = new ArrayList<String>();
    ArrayList<String> checkin = new ArrayList<String>();
    ArrayList<String> sex = new ArrayList<String>();
    ArrayList<String> pull = new ArrayList<String>();
    ArrayList<String> returnDocket = new ArrayList<String>();

    /** Creates new form FrmCurrentAppointment */
    public FrmCurrentAppointment() {
        initComponents();
        loadSpecialist();
        setCalendarDate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbclinic = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblappointments = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSSN = new javax.swing.JTextField();
        txtFirstName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        calDate = new com.toedter.calendar.JDateChooser();
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR, 13); // 1pm
        SpinnerDateModel dateModel = new SpinnerDateModel(calendar.getTime(), null,null, Calendar.HOUR);
        spinner = new JSpinner(dateModel);
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtClinic = new javax.swing.JTextField();
        txtSpecialist = new javax.swing.JTextField();
        cmdReschedule = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        calClinicDate = new com.toedter.calendar.JDateChooser();
        jLabel9 = new javax.swing.JLabel();
        chkDocket = new javax.swing.JCheckBox();
        jLabel10 = new javax.swing.JLabel();
        chkReturn = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(FrmCurrentAppointment.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cmbclinic.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbclinic.setName("cmbclinic"); // NOI18N
        cmbclinic.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cmbclinicMouseClicked(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblappointments.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "SSN", "First Name", "Last Name", "Clinic", "Specialist", "Date", "Time", "Checkin"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblappointments.setName("tblappointments"); // NOI18N
        tblappointments.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblappointmentsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblappointments);
        tblappointments.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title6")); // NOI18N
        tblappointments.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title0")); // NOI18N
        tblappointments.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title1")); // NOI18N
        tblappointments.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title2")); // NOI18N
        tblappointments.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title3")); // NOI18N
        tblappointments.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title4")); // NOI18N
        tblappointments.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title6")); // NOI18N
        tblappointments.getColumnModel().getColumn(7).setHeaderValue(resourceMap.getString("tblappointments.columnModel.title7")); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(FrmCurrentAppointment.class, this);
        jButton2.setAction(actionMap.get("Search")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtSSN.setText(resourceMap.getString("txtSSN.text")); // NOI18N
        txtSSN.setName("txtSSN"); // NOI18N

        txtFirstName.setText(resourceMap.getString("txtFirstName.text")); // NOI18N
        txtFirstName.setName("txtFirstName"); // NOI18N

        txtLastName.setText(resourceMap.getString("txtLastName.text")); // NOI18N
        txtLastName.setName("txtLastName"); // NOI18N

        calDate.setName("calDate"); // NOI18N

        spinner.setName("spinner"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        txtClinic.setText(resourceMap.getString("txtClinic.text")); // NOI18N
        txtClinic.setEnabled(false);
        txtClinic.setName("txtClinic"); // NOI18N

        txtSpecialist.setText(resourceMap.getString("txtSpecialist.text")); // NOI18N
        txtSpecialist.setEnabled(false);
        txtSpecialist.setName("txtSpecialist"); // NOI18N

        cmdReschedule.setAction(actionMap.get("RescheduleAppointment")); // NOI18N
        cmdReschedule.setText(resourceMap.getString("cmdReschedule.text")); // NOI18N
        cmdReschedule.setName("cmdReschedule"); // NOI18N

        jButton3.setAction(actionMap.get("callCheckingPatient")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        calClinicDate.setName("calClinicDate"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        chkDocket.setAction(actionMap.get("Docket")); // NOI18N
        chkDocket.setText(resourceMap.getString("chkDocket.text")); // NOI18N
        chkDocket.setName("chkDocket"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        chkReturn.setAction(actionMap.get("Return")); // NOI18N
        chkReturn.setText(resourceMap.getString("chkReturn.text")); // NOI18N
        chkReturn.setName("chkReturn"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(cmbclinic, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(48, 48, 48)
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(calClinicDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 596, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel6))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(calDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                    .addGap(1, 1, 1)
                                                    .addComponent(txtLastName)))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGap(1, 1, 1)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(txtSSN, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                                                    .addComponent(txtFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))))
                                        .addGap(104, 104, 104))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(194, 194, 194)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chkReturn)
                                    .addComponent(chkDocket)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel8))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtSpecialist)
                                            .addComponent(txtClinic, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)))
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel10))))
                        .addGap(0, 0, 0))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(cmdReschedule)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(cmbclinic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2))
                    .addComponent(calClinicDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel7)
                    .addComponent(txtClinic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel8)
                    .addComponent(txtSpecialist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(calDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(chkDocket))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkReturn)))
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmdReschedule, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        // Code adding the component to the parent container - not shown here
        JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
        DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
        DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
        spinner.setEditor(new DateEditor(spinner, "hh:mm a"));
        // Change the date format to only show the hours
        formatter.setFormat(new SimpleDateFormat("hh:mm a"));
        //formatter.setFormat(new JSpinner.DateEditor(spinner, "hh:mm a"));// SimpleDateFormat("hh:mm a"));
    //formatter.setFormat(new SimpleDateFormat("HH:00 a"));

    jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

    jButton1.setAction(actionMap.get("Exit")); // NOI18N
    jButton1.setName("jButton1"); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(519, 519, 519)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 625, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(26, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addContainerGap())
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-671)/2, (screenSize.height-581)/2, 671, 581);
    }// </editor-fold>//GEN-END:initComponents

    private void cmbclinicMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_cmbclinicMouseClicked
    {//GEN-HEADEREND:event_cmbclinicMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbclinicMouseClicked

    private void tblappointmentsMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tblappointmentsMouseClicked
    {//GEN-HEADEREND:event_tblappointmentsMouseClicked
        // TODO add your handling code here:
        String id_no = tblappointments.getValueAt(tblappointments.getSelectedRow(), 0).toString();
        loadPatientInfo(id_no);
    }//GEN-LAST:event_tblappointmentsMouseClicked

    private void loadPatientInfo(String selected_id) {
        for (int i = 0; i < id.size(); i++) {
            String pt_id = id.get(i);
            if (selected_id.equals(pt_id)) {
                txtSSN.setText(pt_id);
                txtFirstName.setText(firstName.get(i));
                txtLastName.setText(lastName.get(i));
                txtClinic.setText(clinic.get(i));
                txtSpecialist.setText(doctor.get(i));
            }
        }
    }

    private void loadSpecialist() {
        cmbclinic.removeAllItems();
        cmbclinic.addItem(" ");
        String query = "SELECT DISTINCT specialist FROM clinic";
        try {
            PreparedStatement prep = Environment.getDbConn().prepareStatement(query);
            ResultSet rs = prep.executeQuery();

            while (rs.next()) {
                cmbclinic.addItem(rs.getString(1));
            }
            rs.close();
            prep.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void setCalendarDate() {
        calDate.setDate(new java.util.Date());
        calClinicDate.setDate(new java.util.Date());
    }

    @Action
    public void Exit() {
        this.dispose();
    }

    @Action
    public void Search() {
        //create object for date
        DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");
        String newDate = MDY_Formatter.format(calClinicDate.getDate());

        try {

            firstName = new ArrayList<String>();
            lastName = new ArrayList<String>();
            id = new ArrayList<String>();
            clinic = new ArrayList<String>();
            doctor = new ArrayList<String>();
            date = new ArrayList<String>();
            time = new ArrayList<String>();
            checkin = new ArrayList<String>();
            sex = new ArrayList<String>();
            pull = new ArrayList<String>();
            returnDocket = new ArrayList<String>();


            String query = "SELECT Patient_Info.pt_id, Patient_Info.pt_ftname, Patient_Info.pt_ltname, Appointments.clinic, Appointments.specialist,"
                    + "Appointments.date, Appointments.time, Appointments.checkin, Appointments.docket_pull, Appointments.docket_return, Patient_Info.pt_sex "
                    + " FROM Appointments INNER Join Patient_Info ON "
                    + " Patient_Info.pt_id = Appointments.pt_id "
                    + "WHERE Appointments.specialist LIKE ? AND Appointments.date LIKE ? Group By Patient_Info.pt_id, Appointments.specialist, Appointments.clinic,Appointments.date, Appointments.docket_pull, Appointments.docket_return, Appointments.time,"
                    + "Patient_Info.pt_ftname, Patient_Info.pt_ltname, Patient_Info.pt_sex, Appointments.checkin "
                    + "Order by Appointments.specialist";
            PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);
            pstmt.setString(1, cmbclinic.getSelectedItem().toString());
            pstmt.setString(2, newDate);

            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                id.add(rs.getString("pt_id"));
                firstName.add(rs.getString("pt_ftname"));
                lastName.add(rs.getString("pt_ltname"));
                clinic.add(rs.getString("clinic"));
                doctor.add(rs.getString("specialist"));
                date.add(rs.getString("date"));
                time.add(rs.getString("time"));
                checkin.add(rs.getString("Checkin"));
                pull.add(rs.getString("docket_pull"));
                returnDocket.add(rs.getString("docket_return"));

            }


            if (firstName.isEmpty() == false)//If there were results in the query create a table model with the vectors.
            {
                DefaultTableModel model = new DefaultTableModel() {

                    @Override
                    public Class getColumnClass(int columnIndex) {
                        Object o = getValueAt(0, columnIndex);
                        if (o == null) {
                            return Object.class;
                        } else {
                            return o.getClass();
                        }
                    }
                };

                model.addColumn("ID", id.toArray());
                model.addColumn("First Name", firstName.toArray());
                model.addColumn("Last Name", lastName.toArray());
                model.addColumn("Clinic", clinic.toArray());
                model.addColumn("Specialist", doctor.toArray());
                model.addColumn("Date", date.toArray());
                model.addColumn("Time", time.toArray());
                model.addColumn("Checkin", checkin.toArray());
                model.addColumn("Pull", pull.toArray());
                model.addColumn("Return", returnDocket.toArray());
                tblappointments.setModel(model);

            } else //If the query returned no results create a table model that displays a "No Result" message and disables the view salaries register button.
            {
                DefaultTableModel model = new DefaultTableModel() {

                    @Override
                    public boolean isCellEditable(int row, int column) {
                        return false;
                    }

                    @Override
                    public Class getColumnClass(int columnIndex) {
                        Object o = getValueAt(0, columnIndex);
                        if (o == null) {
                            return Object.class;
                        } else {
                            return o.getClass();
                        }
                    }
                };

                id.add("No Results");
                firstName.add("No Results");
                lastName.add("No Results");
                clinic.add("No Results");
                doctor.add("No Results");
                date.add("No Results");
                time.add("No Results");
                checkin.add("No Results");
                pull.add("No Results");
                returnDocket.add("No Results");
                JOptionPane.showMessageDialog(rootPane, "No Appiontments exist for this specialist clinic.");

                model.addColumn("ID", id.toArray());
                model.addColumn("First Name", firstName.toArray());
                model.addColumn("Last Name", lastName.toArray());
                model.addColumn("Clinic", clinic.toArray());
                model.addColumn("Specialist", doctor.toArray());
                model.addColumn("Date", date.toArray());
                model.addColumn("Time", time.toArray());
                model.addColumn("Checkin", checkin.toArray());
                model.addColumn("Pull", pull.toArray());
                model.addColumn("Return", returnDocket.toArray());
                tblappointments.setModel(model);

            }
        } catch (Exception e) {
            String message = "An error occurred while searching for the patients.";
            logger.log(Level.SEVERE, message, e);
        }
    }

    @Action
    public void RescheduleAppointment() {
        //create object for date
        DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");
        DateFormat Time_Formatter = new SimpleDateFormat("hh:mm a");
        String newDate = MDY_Formatter.format(calDate.getDate());
        String newTime = Time_Formatter.format(spinner.getValue());
        String idnumber = txtSSN.getText().toString();

        int n = JOptionPane.showConfirmDialog(null,
                "Are you sure you want to reschedule appointment for - " + tblappointments.getValueAt(tblappointments.getSelectedRow(), 1)
                + "?", "", JOptionPane.YES_NO_CANCEL_OPTION);

        //user click cancel
        if (n == 2) {
            return;
        }
        //yes
        if (n == 0) {
            try {
                //SQl statement to update row from the database
                String query = "Update Appointments SET date = '" + newDate + "', time = '" + newTime + "' Where pt_id = '" + idnumber
                        + "'";
                PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);

                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(rootPane, "Successfully reschedule appointment.");
            } catch (Exception e) {
                String message = "An error occurred while rescheduleing appointment.";
                logger.log(Level.SEVERE, message, e);
            }
        }
        //no
        if (n == 1) {
            return;
        }
    }

    @Action
    public void callCheckingPatient() {
        String id_number = txtSSN.getText().trim(), fname = txtFirstName.getText().trim(), lname = txtLastName.getText().trim(), clinic = txtClinic.getText().trim();
        frmCheckin = new FrmCheckin(id_number, fname, lname, clinic);
        frmCheckin.setLocationRelativeTo(this);
        frmCheckin.setVisible(true);
    }

    /**
     * Places an indictor in the database if the patients medical record has been retrieved.
     */
    @Action
    public void Docket() {
        String Check = "y"; //add flag to database if checkbox was selected
        String ptid = txtSSN.getText();
        String ptclinic = txtClinic.getText();
        //DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");
        //String ptdate = MDY_Formatter.format(calDate.getDate());
        String fname = txtFirstName.getText();
        String lname = txtLastName.getText();


        //if a docket was pulled for the patient place a flag in the database
        if (chkDocket.isSelected()) {
            try {
                String query = "Update Appointments Set docket_pull = '" + Check + "' Where pt_id LIKE '" + ptid + "'AND clinic LIKE '" + ptclinic + "'";
                PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);

                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(rootPane, "Medical Record has been retrieved for " + fname + " " + lname + ".");
                chkDocket.setSelected(false);
            } catch (Exception e) {
                String message = "An error occurred while checking in patient.";
                logger.log(Level.SEVERE, message, e);
            }
        }

    }

    /**
     * Places an indictor in the database if the patients medical record has been returned.
     */
    @Action
    public void Return() {
        String Check = "y"; //add flag to database if checkbox was selected
        String ptid = txtSSN.getText();
        String ptclinic = txtClinic.getText();
        DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");
        String ptdate = MDY_Formatter.format(calDate.getDate());
        String fname = txtFirstName.getText();
        String lname = txtLastName.getText();


        //if a docket was pulled for the patient place a flag in the database
        if (chkReturn.isSelected()) {
            try {
                String query = "Update Appointments Set docket_return = '" + Check + "' Where pt_id LIKE '" + ptid + "'AND clinic LIKE '" + ptclinic + "'";
                PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);

                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(rootPane, "Medical Record has been returned for " + fname + " " + lname + ".");
                chkReturn.setSelected(false);
            } catch (Exception e) {
                String message = "An error occurred while returning patient's file.";
                logger.log(Level.SEVERE, message, e);
            }
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser calClinicDate;
    private com.toedter.calendar.JDateChooser calDate;
    private javax.swing.JCheckBox chkDocket;
    private javax.swing.JCheckBox chkReturn;
    private javax.swing.JComboBox cmbclinic;
    private javax.swing.JButton cmdReschedule;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JSpinner spinner;
    private javax.swing.JTable tblappointments;
    private javax.swing.JTextField txtClinic;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtSSN;
    private javax.swing.JTextField txtSpecialist;
    // End of variables declaration//GEN-END:variables
}
